//
//  CCWechat.m
//  GuessSong-mobile
//
//  Created by 文川 on 2020/9/9.
//

#import "CCWechat.h"
#import "../IosWechat/IosWechat.h"

//#import "cocos2d.h"
//#import "platform/ios/CCEAGLView-ios.h"
#import <cocos/scripting/js-bindings/jswrapper/SeApi.h>
static id<WechatDelegate> gCCWechatWechatDelegate;


@interface CCWechatCallback : NSObject<WechatDelegate> {
}
@end


@implementation CCWechatCallback


//成功获取到了code
-(void)onGetCodeSuccess:(NSString*) code
{
    if(code==nil)
    {
        [self onLoginFail:-1];
    }
    else
    {
        NSString* cmd = [NSString stringWithFormat:@"WechatDelegate.onGetCodeSuccess('%@');",code];
        se::ScriptEngine* se = se::ScriptEngine::getInstance();
        se->evalString([cmd UTF8String]);
    }
}

//登录失败
//1 没有安装微信
//2 授权失败
-(void)onLoginFail:(int) errCode {
    NSString* cmd = [NSString stringWithFormat:@"WechatDelegate.onLoginFail(%d);",errCode];
    se::ScriptEngine* se = se::ScriptEngine::getInstance();
    se->evalString([cmd UTF8String]);
}
@end


//提供给cocos creator的接口
@implementation CCWechat
+(void)go:(NSString*) wxAppId universalLink:(NSString*) universalLink
{
    gCCWechatWechatDelegate = [CCWechatCallback alloc];
    [IosWechat go:wxAppId universalLink:universalLink delegate:gCCWechatWechatDelegate];
}

+(void)login{
    [IosWechat sendAuthRequest];
}
@end
